"""
生成业务流程图的图片文件
需要安装: pip install playwright
然后运行: playwright install chromium
"""

import asyncio
from playwright.async_api import async_playwright
import os

async def generate_image():
    async with async_playwright() as p:
        # 启动浏览器
        browser = await p.chromium.launch()
        page = await browser.new_page()
        
        # 读取HTML文件
        html_file = os.path.join(os.path.dirname(__file__), 'business_flowchart.html')
        await page.goto(f'file:///{html_file}')
        
        # 等待Mermaid渲染完成
        await page.wait_for_timeout(3000)
        
        # 等待Mermaid图表加载
        await page.wait_for_selector('.mermaid', state='visible')
        await page.wait_for_timeout(2000)
        
        # 获取mermaid容器
        mermaid_element = page.locator('.mermaid')
        
        # 截图保存为PNG
        await mermaid_element.screenshot(path='business_flowchart.png', type='png')
        
        # 也可以保存整个页面
        await page.screenshot(path='business_flowchart_full.png', full_page=True)
        
        await browser.close()
        print("流程图图片已生成: business_flowchart.png 和 business_flowchart_full.png")

if __name__ == "__main__":
    try:
        asyncio.run(generate_image())
    except Exception as e:
        print(f"错误: {e}")
        print("请确保已安装 playwright: pip install playwright")
        print("然后运行: playwright install chromium")

